/**
 * 
 */
package edu.umd.clip.lm.model;

import edu.umd.clip.lm.util.tree.*;
import edu.umd.clip.lm.util.*;
import org.w3c.dom.*;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class WordTree<T extends XMLizable> extends XMLizable {
	public static final String XML_ELEMENT_NAME = "word-tree";
	public static final String XML_WORD_FACTOR_ATTR_NAME = "word-factor";
	
	private BinaryTree<T> tree;
	private String wordFactor = "W";
	/**
	 * @return the tree
	 */
	public BinaryTree<T> getTree() {
		return tree;
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.util.XMLizable#createXML(org.w3c.dom.Document)
	 */
	@Override
	public Element createXML(Document doc) {
		Element element = doc.createElement(XML_ELEMENT_NAME);
		element.setAttribute(XML_WORD_FACTOR_ATTR_NAME, wordFactor);
		element.appendChild(tree.createXML(doc));
		return element;
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.util.XMLizable#getElementName()
	 */
	@Override
	public String getElementName() {
		return XML_ELEMENT_NAME;
	}

	/**
	 * @param xmlnode
	 */
	public WordTree(Element xmlnode) {
		assert(xmlnode.getTagName().equals(XML_ELEMENT_NAME));
		wordFactor = xmlnode.getAttribute(XML_WORD_FACTOR_ATTR_NAME);
		assert(wordFactor.length() > 0);
		Element elem = XMLUtils.getFirstElementByTagName(xmlnode, BinaryTree.XML_ELEMENT_NAME);
		this.tree = new BinaryTree<T>(elem);
	}
	
	public WordTree(BinaryTree<T> tree) {
		this.tree = tree;
	}

	/**
	 * @return the wordFactor
	 */
	public String getWordFactor() {
		return wordFactor;
	}

	/**
	 * @param wordFactor the wordFactor to set
	 */
	public void setWordFactor(String wordFactor) {
		this.wordFactor = wordFactor;
	}
	
}
